﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>

using namespace std;


//二分查找
class Solution
{
public:
    int search(vector<int>& nums, int target)
    {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] == target) return mid;
            else if (nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }
        return -1;
    }
};


// 在排序数组中查找元素的第一个和最后一个位置
class Solution
{
public:
    vector<int> searchRange(vector<int>& nums, int target)
    {
        vector<int> ans;
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] == target)
            {
                int j = mid - 1;
                while (j >= 0 && nums[j] == nums[j + 1])
                {
                    j--;
                }
                ans.push_back(j + 1);
                j = mid + 1;
                while (j <= right && nums[j] == nums[j - 1])
                {
                    j++;
                }
                ans.push_back(j - 1);
                return ans;
            }
            else if (nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }

        return { -1,-1 };
    }
};


class Solution
{
public:
    vector<int> searchRange(vector<int>& nums, int target)
    {
        if (nums.size() == 0) return { -1,-1 };
        int begin = 0, left = 0, right = nums.size() - 1;
        // 寻找左端点--分段为小于和大于等于
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        if (nums[left] != target) return { -1,-1 };
        begin = left;
        // 寻找右端点--分段为小于等于和大于
        right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] <= target) left = mid;
            else right = mid - 1;
        }

        return { begin,right };
    }
};

//搜索插入位置
class Solution
{
public:
    int searchInsert(vector<int>& nums, int target)
    {
        int left = 0, right = nums.size() - 1;
        // 插入位置为index,[left,index - 1] < target
        // [index,right] >= target
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else right = mid;
        }
        if (nums[right] < target) return right + 1;
        return right;
    }
};

//x的平方根
class Solution
{
public:
    int mySqrt(int x)
    {
        if (x < 1) return 0;
        int left = 1, right = x;
        while (left < right)
        {
            long long mid = left + (right - left + 1) / 2;
            if (mid * mid <= x)
            {
                left = mid;
            }
            else right = mid - 1;
        }

        return left;
    }
};

//山脉数组的峰顶索引
class Solution
{
public:
    int peakIndexInMountainArray(vector<int>& arr)
    {
        int left = 0, right = arr.size() - 2;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (arr[mid] > arr[mid - 1]) left = mid;
            else right = mid - 1;
        }

        return left;
    }
};


//寻找峰值
class Solution
{
public:
    int findPeakElement(vector<int>& nums)
    {
        // arr[i] > arr[i + 1] ：此时「左侧区域」⼀定会存在⼭峰（因为最左侧是负⽆
        // 穷），那么我们可以去左侧去寻找结果；
        // arr[i] < arr[i + 1] ：此时「右侧区域」⼀定会存在⼭峰（因为最右侧是负⽆
        // 穷），那么我们可以去右侧去寻找结果。
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid + 1]) right = mid;
            else left = mid + 1;
        }

        return left;
    }
};

//▪ 当 mid 在[A，B] 区间的时候，也就是 mid 位置的值严格⼤于 D 点的值，下⼀次查
//询区间在[mid + 1，right] 上；
//▪ 当 mid 在[C，D] 区间的时候，也就是 mid 位置的值严格⼩于等于 D 点的值，下次
//查询区间在[left，mid] 上。
class Solution
{
public:
    int findMin(vector<int>& nums)
    {
        int left = 0, right = nums.size() - 1;
        // A-B区间大于最后的元素,C-D区间小于等于最后一个元素
        // 以最后一个元素作为参照值
        int x = nums[right];
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > x) left = mid + 1;
            else right = mid;
        }

        return nums[left];
    }
};


// 点名
class Solution
{
public:
    int takeAttendance(vector<int>& records)
    {
        // 在第⼀个缺失位置的左边，数组内的元素都是与数组的下标相等的；
        // 在第⼀个缺失位置的右边，数组内的元素与数组下标是不相等的。
        int left = 0, right = records.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (records[mid] == mid) left = mid + 1;
            else right = mid;
        }
           
        //只有一个元素的时候，特殊情况
        return left == records[left] ? left + 1 : left;
    }
};
int main()
{
	return 0;
}

